package com.linkstec.bookshop.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.linkstec.bookshop.mapper.GoodsMapper;
import com.linkstec.bookshop.pojo.Goods;
import com.linkstec.bookshop.service.GoodsService;
import com.linkstec.bookshop.util.PageBean;
import com.linkstec.bookshop.vo.GoodsVo;

@Service
public class GoodsServiceImpl implements GoodsService {

	@Autowired
	private GoodsMapper goodsMapper;

	@Override
	public List<GoodsVo> findHot() throws RuntimeException {
		return goodsMapper.findHot();
	}

	@Override
	public PageBean<GoodsVo> findGoodsByCategory(int cgId, int page) throws RuntimeException {

		PageBean<GoodsVo> goodsBean = new PageBean<GoodsVo>();

		goodsBean.setPage(page);// 第几页

		int limitPage = 8;
		goodsBean.setLimitPage(limitPage);// 设置每页8个

		int count = goodsMapper.selGoodsCountByCategory(cgId);// 查询数量
		int totlePage = 0;

		if (Math.ceil(count % limitPage) == 0) {
			totlePage = count / limitPage;
		} else {
			totlePage = count / limitPage + 1;
		}
		goodsBean.setTotlePage(totlePage);// 设置总页数

		int beginPage = (page - 1) * limitPage;// 开始

		List<GoodsVo> list = goodsMapper.findGoodsByCategory(cgId, beginPage, limitPage);

		goodsBean.setList(list);
		return goodsBean;
	}

	@Override
	public PageBean<GoodsVo> findGoodsVague(int page, String condition) throws RuntimeException {

		PageBean<GoodsVo> goodsBean = new PageBean<GoodsVo>();

		goodsBean.setPage(page);// 第几页

		int limitPage = 8;
		goodsBean.setLimitPage(limitPage);// 设置每页8个

		int count = goodsMapper.findGoodsCountVague(condition);

		int totlePage = 0;

		if (Math.ceil(count % limitPage) == 0) {
			totlePage = count / limitPage;
		} else {
			totlePage = count / limitPage + 1;
		}
		goodsBean.setTotlePage(totlePage);// 设置总页数

		int beginPage = (page - 1) * limitPage;// 开始

		List<GoodsVo> list = goodsMapper.findGoodsVague(condition, beginPage, limitPage);

		goodsBean.setList(list);
		return goodsBean;
	}

	@Override
	public GoodsVo findOneGoodsDetail(int goodsId) throws RuntimeException {

		return goodsMapper.findOneGoodsDetail(goodsId);
	}

	@Override
	public PageBean<GoodsVo> findAllGoods(int page) throws RuntimeException {
		PageBean<GoodsVo> goodsBean = new PageBean<GoodsVo>();

		goodsBean.setPage(page);// 第几页

		int limitPage = 10;
		goodsBean.setLimitPage(limitPage);// 设置每页10个

		int count = goodsMapper.findAllGoodsCount();

		int totlePage = 0;

		if (Math.ceil(count % limitPage) == 0) {
			totlePage = count / limitPage;
		} else {
			totlePage = count / limitPage + 1;
		}
		goodsBean.setTotlePage(totlePage);// 设置总页数

		int beginPage = (page - 1) * limitPage;// 开始

		List<GoodsVo> list = goodsMapper.findAllGoods(beginPage, limitPage);

		goodsBean.setList(list);
		return goodsBean;
	}

	@Transactional
	@Override
	public void update(Goods goods, int addNum) throws RuntimeException {
		goods.setNumber(addNum + goods.getNumber());
		goodsMapper.update(goods);
	}

	@Transactional
	@Override
	public void goodsOffline(int goodsId) throws RuntimeException {
		// TODO Auto-generated method stub
		goodsMapper.goodsOffline(goodsId);
	}

	@Transactional
	@Override
	public void goodsOnline(int goodsId) throws RuntimeException {
		// TODO Auto-generated method stub
		goodsMapper.goodsOnline(goodsId);
	}

	@Override
	public void add(int bookId) throws RuntimeException {
		// TODO Auto-generated method stub
		goodsMapper.add(bookId);
	}

	@Override
	public GoodsVo findOneGoodsDetailById(int goodsId) throws RuntimeException {
		// TODO Auto-generated method stub
		return goodsMapper.findOneGoodsDetailById(goodsId);
	}

}
